/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testsystemtray;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 *
 * @author ahmet
 */
public class Main extends JFrame implements Runnable{

    private TrayIcon ti;
    private Image icon1, icon2;
    public Main() {
        icon1 = Toolkit.getDefaultToolkit().getImage("good.gif");
        icon2 = Toolkit.getDefaultToolkit().getImage("bad.gif");
        ti = new TrayIcon(icon1, "posture correction system");

        ti.setImageAutoSize(true);
        try {
            SystemTray.getSystemTray().add(ti);
        } catch (AWTException ex) {
            ex.printStackTrace();
        }

        ti.displayMessage("Warning", "Please correct your posture.", TrayIcon.MessageType.WARNING);

        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent we) {
                super.windowClosing(we);
                SystemTray.getSystemTray().remove(ti);
                t=null;
            }

        });


        setSize(400, 300);
        setVisible(true);

        if (t==null) {
            t=new Thread(this);
            t.start();
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        new Main();
    }

    Thread t;
    boolean green=true;
    public void run() {
        while(true) {
            if(green) {
                ti.setImage(icon2);
            } else{
                ti.setImage(icon1);
            }
            green = !green;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

}
